﻿Imports System.IO
Imports System.Data.SqlClient

Public Class FrmLuuHoSoScan
    Public m_tennhap As String
    Dim tenFileSuDung As String = ""
    Dim prcss As Process
    Dim tenFileGrid As String = ""
    Dim dirTemp As String = ""
    Private Sub btnChon_Click(sender As Object, e As EventArgs) Handles btnChon.Click
        openFileDialog.FileName = ""
        openFileDialog.ShowDialog()
        If openFileDialog.FileName = "openFileDialog" Then
            openFileDialog.FileName = ""
        End If
        txtDuongDan.Text = openFileDialog.FileName
    End Sub

    Private Sub btnLuu_Click(sender As Object, e As EventArgs) Handles btnLuu.Click
        If txtDuongDan.Text = "" Then
            MessageBox.Show("Chọn lại đường dẫn file scan")
            Return
        End If
        If TextBoxSoMucLuc.Text = "" Then
            MessageBox.Show("Chọn tên tài liệu kết nối ")
            Return
        End If
        If KiemTraFile(Integer.Parse(txtSoHoSo.Text), txtDuongDan.Text) = True Then
            MessageBox.Show("File bạn đã chọn đã có sẵn")
            Return
        End If
        SavePDFtoDB()
        CapNhatDulieu()
        TextBoxSoMucLuc.Text = ""
        txtDuongDan.Text = ""
        txtTenVanBan.Text = ""
        NapDataGridViewVanBan()
        NapDanhSachScan()
        'StorePicture(Integer.Parse(txtSoHoSo.Text), txtDuongDan.Text)
        'dgvDanhSach.DataSource = LoadVaoGridview(Integer.Parse(txtSoHoSo.Text))
        'dgvDanhSach.Refresh()
    End Sub
    Private Sub SavePDFtoDB()
        Try
            ob_cnn.Open()
            Dim tennhap As String
            tennhap = ModuleTenDangNhap.m_tennhap
            Dim imageData As Byte() = Nothing
            Using fs As New FileStream(txtDuongDan.Text, FileMode.Open, FileAccess.Read)
                imageData = New Byte(fs.Length - 1) {}
                fs.Read(imageData, 0, CInt(fs.Length))
            End Using

            Dim Sql As String = ""
            Sql = "INSERT INTO LuuTepScanBienDong (sohoso,SoMucLuc,TenTep, NgayLuu, NguoiNhap, NoiDung )VALUES(@sohoso, @SoMucLuc, @TenTep, @NgayLuu,@NguoiNhap, @NoiDung)"
            Dim sqlquery As New SqlCommand(Sql, ob_cnn)
            sqlquery.Parameters.Add(New System.Data.SqlClient.SqlParameter("@sohoso", txtSoHoSo.Text))
            sqlquery.Parameters.Add(New System.Data.SqlClient.SqlParameter("@SoMucLuc", TextBoxSoMucLuc.Text))
            sqlquery.Parameters.Add(New System.Data.SqlClient.SqlParameter("@TenTep", txtDuongDan.Text))
            sqlquery.Parameters.Add(New System.Data.SqlClient.SqlParameter("@NgayLuu", DateTime.Now))
            sqlquery.Parameters.Add(New System.Data.SqlClient.SqlParameter("@NguoiNhap", tennhap))
            sqlquery.Parameters.Add(New System.Data.SqlClient.SqlParameter("@NoiDung", imageData))
            sqlquery.ExecuteNonQuery()
            ob_cnn.Close()
            MsgBox("Đã lưu thành công  !!")
        Catch err As Exception
            MsgBox(err.Message)
        End Try
    End Sub
    Private Sub CapNhatDulieu()
        ob_cnn.Open()
        Dim Sql As String = ""
        Sql = "Update MucLucBienDong SET KetNoiScan = '1'  WHERE MamuclucID = '" & TextBoxSoMucLuc.Text & "'"
        Dim cmd As New SqlCommand(Sql, ob_cnn)
        cmd.ExecuteNonQuery()
        ob_cnn.Close()
    End Sub
    Private Sub btnThoat_Click(sender As Object, e As EventArgs) Handles btnThoat.Click
        XoafileTam(dirTemp)
        Me.Close()

    End Sub

    Private Sub FrmLuuHoSoScan_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        txtSoHoSo.Text = _sohoso
        NapDataGridViewVanBan()
        NapDanhSachScan()

        DataGridViewMucLuc.RowsDefaultCellStyle.BackColor = Color.Beige
        DataGridViewMucLuc.AlternatingRowsDefaultCellStyle.BackColor = Color.Azure
        dgvDanhSach.RowsDefaultCellStyle.BackColor = Color.Beige
        dgvDanhSach.AlternatingRowsDefaultCellStyle.BackColor = Color.Azure
        'dgvDanhSach.DataSource = LoadVaoGridview(Integer.Parse(txtSoHoSo.Text))
        'dgvDanhSach.Refresh()
    End Sub
    Private Sub NapDanhSachScan()
        Dim sql As String
        sql = "select maid, sohoso, soMucLuc, TenTep from LuuTepScanBienDong where sohoso = '" & txtSoHoSo.Text & "'"
        dgvDanhSach.DataSource = LoadThongTin(sql)
        dgvDanhSach.Refresh()
        ob_cnn.Close()
    End Sub
    Private Sub NapDataGridViewVanBan()
        Dim sql As String
        sql = "select  ml.KyHieuVanBan, ml.ngayky, vb.TenVanBan, ml.BanGoc, ml.BanSao, MamuclucID " &
                                " From MucLucBienDong ml,  DMTenVanBan vb" &
                                " where ml.tenloaivanbanid= vb.maTenvanban " &
                                " and ml.sohoso ='" & txtSoHoSo.Text & "' " &
                                " and KetNoiScan = 0 ORDER BY MamuclucID "

        DataGridViewMucLuc.DataSource = LoadThongTin(sql)
        DataGridViewMucLuc.Refresh()
        ob_cnn.Close()
    End Sub

    Private Sub dgvDanhSach_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvDanhSach.CellClick
        Try

            tenFileGrid = dgvDanhSach.Rows(e.RowIndex).Cells("duongdan").Value.ToString()
            dirTemp = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
            Dim img As Byte() = RetrieveImage2(tenFileGrid, Integer.Parse(txtSoHoSo.Text))
            If Not Directory.Exists(dirTemp & "\\Quan Ly Kho") Then
                Directory.CreateDirectory(dirTemp & "\\Quan Ly Kho")
            End If
            tenFileSuDung = dirTemp & "\\Quan Ly Kho\\" & tenFileGrid.Split("\\").Last()



            File.WriteAllBytes(tenFileSuDung, img)
            AxAcroPDF3.src = tenFileSuDung

            'Dim proc As New ProcessStartInfo()
            'proc.WindowStyle = ProcessWindowStyle.Normal
            'proc.FileName = tenFileSuDung
            'prcss = Process.Start(proc)

        Catch ex As Exception
            If ex.Message.Contains("because it is being used by another process") Then
                MessageBox.Show("File bạn đang mở !")
            End If
        End Try
    End Sub

    Private Sub dgvDanhSach_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvDanhSach.CellContentClick

    End Sub

    Private Sub DataGridViewLucMuc_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridViewMucLuc.CellContentClick
        Dim dong As Integer
        dong = e.RowIndex
        'Kiem tra phai dong tieu de ko
        If (dong = -1) Then
            ' MessageBox.Show("Không tìm thấy thông tin !")
            Return
        End If
        TextBoxSoMucLuc.DataBindings.Clear()
        TextBoxSoMucLuc.DataBindings.Add("Text", DataGridViewMucLuc.DataSource, "MamuclucID")
        txtTenVanBan.DataBindings.Clear()
        txtTenVanBan.DataBindings.Add("Text", DataGridViewMucLuc.DataSource, "Tenvanban")

    End Sub

  
End Class